package com.jat.algorithms.sort;

import java.util.Arrays;

/**
 * @author jatchen
 * @description: 冒泡排序
 * @date 2021/9/2811:41 PM
 */
public class BubbleSort {

    public static void main(String[] args) {
        int[] array = {3,1,4,2,5,9,7};
        bubble(array);
        System.out.println(Arrays.toString(array));
    }

    /**
     * 两两比较，将最大的放最后
     * @param array
     */
    private static void bubble(int[] array){
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length-i-1; j++) {
                if (array[j] > array[j+1]){
                    int swap = array[j];
                    array[j] = array[j+1];
                    array[j+1] = swap;
                }
            }
        }
    }
}
